import 'dart:ffi';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:wechat/pages/Profile/profile_page.dart';
import 'package:wechat/pages/discover/discover_cell.dart';

class ProfilePage extends StatefulWidget {
  const ProfilePage({Key? key}) : super(key: key);

  @override
  State<ProfilePage> createState() => _ProfilePageState();
}

class _ProfilePageState extends State<ProfilePage> with AutomaticKeepAliveClientMixin<ProfilePage>{
  late File _imageFile;
  bool isShowSelected = false;
  bool isShowPhoto = false;

  void _callCamera() async {
    print("调用相机");
    XFile? file = await ImagePicker().pickImage(source: ImageSource.camera);
    if (file != null) {
      setState(() {
        isShowSelected = true;
        isShowPhoto = false;
        _imageFile = File(file!.path);
      });
    }
  }
  void _callPhoto () async {
    print("调用相册");
    XFile? file = await ImagePicker().pickImage(source: ImageSource.gallery);
    if (file != null) {
      setState(() {
        isShowPhoto = true;
        isShowSelected = false;
        _imageFile = File(file!.path);
      });
    }
  }
  DecorationImage? _showImage() {
    if (isShowSelected || isShowPhoto) {
      return DecorationImage(image: FileImage(_imageFile),fit: BoxFit.cover);
    }else{
      return DecorationImage(image: AssetImage("images/profile/锡兵.png"),);
    }
  }
  Widget _headerWidget() {
    return Container(
      color: Colors.white,
      height: 200,
      child: Stack(
        children: [
          Positioned(
            left: 0,
            top: 50,
            child: GestureDetector(
              onTap: _callPhoto,
              child: Container(
                width: 100,
                height: 100,
                child: DecoratedBox(
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(20),
                    //image: DecorationImage(image: AssetImage("images/profile/锡兵.png"),),
                    image: _showImage(),
                  ),
                ),
                margin: EdgeInsets.all(15),
              ),
            ),
          ),
          Positioned(
            left: 130,
            top: 65,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  "锡兵-坚定的锡兵",
                  style: TextStyle(fontWeight: FontWeight.bold, fontSize: 22),
                ),
                Text("锡兵-坚定的锡兵"),
                Text("锡兵-坚定的锡兵"),
              ],
            ),
          ),
          Positioned(
            right: 10,
            top: 100,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                Image(
                  image: AssetImage("images/profile/二维码.png"),
                  width: 20,
                  height: 20,
                ),
                Icon(Icons.keyboard_arrow_right),
              ],
            ),
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
      body: Container(
        child: Stack(
          children: [
            Container(
              child: MediaQuery.removePadding(
                context: context,
                child: ListView(
                  children: [
                    _headerWidget(),
                    SizedBox(height: 15,),
                    DiscoverCell(imageName: "images/profile/微信支付.png", title: "服务"),
                    SizedBox(height: 15,),
                    DiscoverCell(imageName: "images/profile/微信收藏.png", title: "收藏"),
                    Stack(
                      children: [
                        Container(
                          height: 0.5,
                          color: Colors.grey,
                        ),
                        Container(
                          color: Colors.white,
                          width: 35,
                          height: 0.5,
                        )
                      ],
                    ),
                    DiscoverCell(imageName: "images/profile/我的朋友圈.png", title: "朋友圈"),
                    Stack(
                      children: [
                        Container(
                          height: 0.5,
                          color: Colors.grey,
                        ),
                        Container(
                          color: Colors.white,
                          width: 35,
                          height: 0.5,
                        )
                      ],
                    ),
                    DiscoverCell(imageName: "images/profile/微信卡包.png", title: "卡包"),
                    Stack(
                      children: [
                        Container(
                          height: 0.5,
                          color: Colors.grey,
                        ),
                        Container(
                          color: Colors.white,
                          width: 35,
                          height: 0.5,
                        )
                      ],
                    ),
                    DiscoverCell(imageName: "images/profile/微信表情.png", title: "表情"),
                    SizedBox(height: 15,),
                    DiscoverCell(imageName: "images/profile/微信设置.png", title: "设置"),
                  ],
                ),
                removeTop: true,
              ),
              color: Color.fromRGBO(220, 220, 220, 1),
            ),
            Positioned(
              right: 10,
              top: 40,
              child: GestureDetector(
                onTap: _callCamera,
                child: Container(
                  width: 25,
                  child: Image(image: AssetImage("images/profile/相机.png"),),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;

  @override
  void dispose() {
    print("我的界面销毁了");
    super.dispose();
  }
}
